package com.goods.business.controller;

import com.goods.business.service.ProductCategoryService;
import com.goods.common.response.ResponseBean;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 业务管理模块
 * @Author jiaoyanlin
 * @Date 2021/11/8 11:02
 * @Description MangeController
 */
@RestController
@RequestMapping("/business/productCategory")
public class MangeController {

    @Autowired
    ProductCategoryService productCategoryService;
    //加载分类数据
///categoryTree?pageNum=1&pageSize=5 ;PageInfo<>
   @GetMapping("/categoryTree")
    public ResponseBean<PageVO<ProductCategoryTreeNodeVO>> getCategoryList(
            @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
            @RequestParam(value = "pageSize",defaultValue = "5")Integer pageSize
           ){
       PageVO<ProductCategoryTreeNodeVO> pageVO=  productCategoryService.getCategoryList(pageNum,pageSize);

       return ResponseBean.success(pageVO);
   }
//加载父级分类数据
   @GetMapping("/getParentCategoryTree")
    public ResponseBean<List<ProductCategoryTreeNodeVO>> getParentCategoryTree(){

       List<ProductCategoryTreeNodeVO> list= productCategoryService.getParentCategoryTree();
       return ResponseBean.success(list);
   }
///add 添加分类
    @PostMapping("/add")
    public ResponseBean add(@RequestBody ProductCategoryTreeNodeVO productCategoryTreeNodeVO){
        try {
            productCategoryService.add(productCategoryTreeNodeVO);
            return ResponseBean.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseBean.error(e);
        }
    }
    ///edit/34回显修改内容
    @GetMapping("/edit/{id}")
    public ResponseBean<ProductCategoryTreeNodeVO> edit(@PathVariable Integer id){
        ProductCategoryTreeNodeVO productCategoryTreeNodeVO = productCategoryService.edit(id);
            return ResponseBean.success(productCategoryTreeNodeVO);
    }

    ///update/34修改
    @PutMapping("/update/{id}")
    public ResponseBean update(@PathVariable Long id,
                               @RequestBody ProductCategoryTreeNodeVO productCategoryTreeNodeVO ){
        try {
            productCategoryService.update(id,productCategoryTreeNodeVO);
            return ResponseBean.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseBean.error(e);
        }

    }

 //delete/20
    @DeleteMapping("/delete/{id}")
    public ResponseBean delete(@PathVariable Long id){

           Boolean falg = productCategoryService.update(id);
           if (falg)
                 return ResponseBean.success();
            else
                 return ResponseBean.error(falg);


    }

}
